package de.tum.in.eist.poll.client;

import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.pub.shared.Feedback;
import de.tum.in.eist.poll.pub.shared.Lecture;
import de.tum.in.eist.poll.pub.shared.Student;

/**
 * This Object contains the poll
 */
public class giveFeedbackView extends VerticalPanel {
	private int selected = 5;
	RadioButton veryUnsatisfied = new RadioButton("satisfaction");
	RadioButton unsatisfied = new RadioButton("satisfaction");
	RadioButton neutral = new RadioButton("satisfaction");
	RadioButton satisfied = new RadioButton("satisfaction");
	RadioButton verySatisfied = new RadioButton("satisfaction");
	TextArea like = new TextArea();
	TextArea dislike = new TextArea();
	Button submit = new Button();
	Button back = new Button();
	Label likeLabel = new Label("What did you like?");
	Label dislikeLabel = new Label("What did you dislike?");
	ClickHandler handler = new SelectHandler();

	/**
	 * Generates a new giveFeedbackView-Object
	 * 
	 * @param lecture is the Lecture the current user(student) tries to commit on.
	 * @param student is the current user, who tries to commit on the poll.
	 */
	 
	public giveFeedbackView(final Lecture lecture, final Student student) {
		AsyncCallback<List<Feedback>> callback = new AsyncCallback<List<Feedback>>() {

			@Override
			public void onSuccess(List<Feedback> result) {
				if (result != null) {
					ListIterator<Feedback> iterator = result.listIterator();
					while (true) {
						try {
							Feedback feedback = iterator.next();
							if (feedback.getStudent().getName().compareTo(student.getName()) == 0) {
								showAlreadyCommitedError(lecture, student);
								break;
							}
						}
						catch (NoSuchElementException e) {
							buildView(lecture, student);
							break;
						}
					}
				}
				else{
					buildView(lecture, student);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
		EIST_POLL.service.getFeedback(lecture, callback);
	}

	/**
	 * Opens a Dialog-Box, if the student tries to commit on an already commited poll again.
	 * 
	 * @param lecture is the Lecture the current user(student) has already commited on.
	 * @param student is the current user, who tries to commit on the commited poll.
	 */
	private void showAlreadyCommitedError(Lecture lecture, Student student) {
		final DialogBox error = new DialogBox();
		error.setText("Error");
		VerticalPanel messagePanel = new VerticalPanel();
		messagePanel.add(new Label("You've commited on this lecture already!"));
		Button ok = new Button();
		ok.setText("OK");
		ok.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				error.hide();
				RootPanel root = RootPanel.get("main");
				root.clear();
				root.add(new SelectLectureView());
			}
		});
		messagePanel.add(ok);
		error.add(messagePanel);
		error.center();
	}

	/**
	 * Builds the poll 
	 * 
	 * @param lecture is the Lecture the current user(student) tries to commit on.
	 * @param student is the current user, who tries to commit on the poll.
	 */
	private void buildView(final Lecture lecture, final Student student) {
		submit.setText("Submit");
		submit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (selected != 5) {
					AsyncCallback<String> callback = new AsyncCallback<String>() {

						@Override
						public void onSuccess(String result) {
							final DialogBox message = new DialogBox();
							message.setText("Success");
							VerticalPanel content = new VerticalPanel();
							content.add(new Label(result));
							Button ok = new Button();
							ok.setText("OK");
							ok.addClickHandler(new ClickHandler() {

								@Override
								public void onClick(ClickEvent event) {
									message.hide();
									RootPanel root = RootPanel.get("main");
									root.clear();
									RootPanel.get("back").clear();
									root.add(new StatisticView(lecture));
								}
							});
							content.add(ok);
							message.add(content);
							message.center();
						}

						@Override
						public void onFailure(Throwable caught) {
							final DialogBox error = new DialogBox();
							error.setText("ERROR");
							VerticalPanel content = new VerticalPanel();
							content.add(new Label(caught.toString()));
							Button ok = new Button();
							ok.setText("Close");
							ok.addClickHandler(new ClickHandler() {

								@Override
								public void onClick(ClickEvent event) {
									error.hide();
								}
							});
							content.add(ok);
							error.add(content);
							error.center();
						}
					};
					EIST_POLL.service.addFeedback(new Feedback(0, lecture, student,selected,like.getText(),dislike.getText()), callback);
				} else {
					final DialogBox error = new DialogBox();
					error.setText("ERROR");
					VerticalPanel content = new VerticalPanel();
					content.add(new Label("Please Select an answer!"));
					Button ok = new Button();
					ok.setText("Close");
					ok.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {
							error.hide();
						}
					});
					content.add(ok);
					error.add(content);
					error.center();
				}
			}
		});
		veryUnsatisfied.setText("Very unsatisfied");
		veryUnsatisfied.addClickHandler(handler);
		unsatisfied.setText("Unsatisfied");
		unsatisfied.addClickHandler(handler);
		neutral.setText("Neutral");
		neutral.addClickHandler(handler);
		satisfied.setText("Satisfied");
		satisfied.addClickHandler(handler);
		verySatisfied.setText("Very satisfied");
		verySatisfied.addClickHandler(handler);

		this.add(veryUnsatisfied);
		this.add(unsatisfied);
		this.add(neutral);
		this.add(satisfied);
		this.add(verySatisfied);
		this.add(likeLabel);
		this.add(like);
		this.add(dislikeLabel);
		this.add(dislike);
		this.add(submit);
		
		/**
		 * Adds the Back-Button
		 */
		back.setText("Back");
		back.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				RootPanel root = RootPanel.get("main");
				root.clear();
				root.add(new SelectLectureView());
				RootPanel.get("back").clear();
			}
		});
		
		RootPanel.get("back").add(back);
	}

	/**
	 * This class is used to implement the onClick-Funktion
	 */
	private class SelectHandler implements ClickHandler {

		/**
		 * The selected Radio-Button gets saved.
		 */
		@Override
		public void onClick(ClickEvent event) {
			if (event.getSource().equals(veryUnsatisfied))
				selected = 0;
			if (event.getSource().equals(unsatisfied))
				selected = 1;
			if (event.getSource().equals(neutral))
				selected = 2;
			if (event.getSource().equals(satisfied))
				selected = 3;
			if (event.getSource().equals(verySatisfied))
				selected = 4;
		}
	}
}
